<template>
  <div>
    <nav-bar-back>
      <img
        class="left"
        src="../../assets/img/navbar/ic_title_close.png"
        slot="left"
        alt=""
        @click="$router.back()"
      />
      <span slot="center">BT游戏</span>
      <div slot="right" class="right">
        <img
          src="../../assets/img/navbar/ic_header_search.png"
          alt=""
          @click="serachClick"
        />
        <img
          src="../../assets/img/navbar/ic_header_preparation.png"
          alt=""
          @click="screenShowClick"
        />
      </div>
    </nav-bar-back>
    <div class="content_box" ref="viewhome">
      <van-pull-refresh
        v-model="isLoading"
        :head-height="88"
        @refresh="onRefresh"
      >
        <van-list
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="onLoad"
          :immediate-check="false"
        >
          <game-list :list="list" @detail-click="detailClick" />
        </van-list>
      </van-pull-refresh>
      <back-top :scrollTop="scTop" @back-top="backTop"/>
    </div>
    <!-- </scroll> -->
    <div class="bottom_btn" v-show="!typeList.bt" @click="typeClose"></div>
    <search :show="show" @on-search="onSearch" @close-search="closeSearch" />
    <screen
      :show="isShow"
      @show-click="showClick"
      @screen-click="screenClick"
      :clientArr="clientArr"
      :genreList="genreList"
    />
  </div>
</template>

<script>
import NavBarBack from "@/components/common/navbar/NavBarBack.vue";
import Search from "@/components/common/search/Search.vue";
import Screen from "@/components/common/screen/Screen.vue";
import GameList from "../../components/content/gamelList/GameList.vue";
import { getGameList } from "../../network/home";
import storage from '../../common/storage'
import BackTop from '../../components/common/backTop/BackTop.vue';
import mixinBackTop from '../../mixins/common'

export default {
  name: 'BtGame',
  mixins: [mixinBackTop],
  components: {
    NavBarBack,
    Search,
    Screen,
    GameList,
    BackTop,
  },
  data() {
    return {
      show: false, //搜索弹窗
      isShow: false, //筛选弹窗
      isLoading: false,
      loading: true,
      finished: false,
      list: [],
      clientArr: [],
      genreList: [],
      currentType: 1,
      page: 1,
      clientType: "",
      genreId: "",
      typeList: {},
      tgid: '',
      scrollTop: '',
    };
  },
  //在页面离开时记录滚动位置
  beforeRouteLeave (to, from, next) {
    this.scrollTop = document.documentElement.scrollTop || document.querySelector('.content_box').scrollTop
    console.log(this.scrollTop)
    next()
  },
  activated() {
    const top = document.querySelector('.content_box')
    top.scrollTo(0, this.scrollTop);
    this.typeList = storage.get(this.tgid)
  },
  created() {
    this.getBtGame()
    this.tgid = this.$cookies.get('tgid')
    this.typeList = storage.get(this.tgid)
  },
  methods: {
    getBtGame() {
      this.finished = false;
      getGameList(
        this.currentType,
        this.page,
        this.clientType,
        this.genreId
      ).then((res) => {
        this.isLoading = false;
        if (res.state === "ok") {
          console.log(res.data);
          this.clientArr = res.data.client_arr;
          this.genreList = res.data.genre_list;
          this.list = [...this.list, ...res.data.list];
          this.loading = false;
          if (res.data.list === null) {
            this.finished = true;
          }
        } else if (res.msg === "no_login") {
          this.$router.replace("/login");
        }
      });
    },
    screenClick(type, id) {
      this.isShow = false;
      this.clientType = type;
      this.genreId = id;
      this.list = [];
      this.page = 1;
      this.getBtGame();
    },
    detailClick(id) {
      this.$router.push("/gameinfo/gameid/" + id)
    },
    serachClick() {
      this.show = true;
    },
    screenShowClick() {
      this.isShow = true;
    },
    closeSearch() {
      this.show = false;
    },
    onSearch(val) {
      //发起网络请求
      console.log(val);
    },
    onLoad() {
      this.page++;
      this.getBtGame();
    },
    onRefresh() {
      this.list = [];
      this.page = 1;
      this.getBtGame();
    },
    showClick() {
      this.isShow = !this.isShow;
    },
    typeClose() {
      this.typeList.bt = true
      this.$toast('已经添加到首页导航区')
      storage.set(this.tgid,this.typeList)
    },
  },
};
</script>

<style scoped>
.left {
  width: 44px;
  height: 44px;
}

.right {
  display: flex;
  align-items: center;
}

.right img {
  width: 44px;
  height: 44px;
  margin-left: 20px;
}

.content_box {
  width: 750px;
  padding: 0 0 100px;
  margin-top: 88px;
  margin-left: 50%;
  transform: translateX(-50%);
}

.list_box {
  width: 750px;
}

.list_item {
  width: 694px;
  height: 150px;
  padding: 20px 28px;
  border-bottom: 10px solid #ededed;
  display: flex;
}

.gameicon {
  width: 150px;
  height: 150px;
  overflow: hidden;
  margin-right: 20px;
}

.game_content {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.content_flex {
  display: flex;
  justify-content: space-between;
}

.game_name {
  color: #333;
  font-size: 28px;
  font-weight: bold;
  align-items: center;
}

.gamename_over {
  width: 405px;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}

.game_name .btn {
  width: 115px;
  height: 48px;
  font-weight: 500;
  font-size: 24px;
  color: #3549ea;
  border: 1px solid #3549ea;
}

.genre_str {
  color: #515151;
  font-size: 24px;
}

.game_status {
  color: #515151;
  font-size: 24px;
}

.gameicon img {
  width: 100%;
  height: 100%;
}

.list_date::before {
  content: "";
  position: absolute;
  top: 8px;
  right: 150px;
  background: url("../../assets/img/page/ic_cash_time.png") no-repeat;
  background-size: 100%;
  width: 24px;
  height: 24px;
}

.bottom_btn {
  width: 750px;
  height: 88px;
  background: #fff;
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: 0;
  padding-bottom: env(safe-area-inset-bottom);
}

.bottom_btn::before {
  content: "";
  width: 44px;
  height: 44px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: url("../../assets/img/page/ic_bottom_add.png") no-repeat;
  background-size: 100%;
}
</style>